provider "alicloud" {
  profile = "default"
  version = "1.151.0"
}

provider "alicloud" {
  profile = "xiaoqi"
  version = "1.151.0"
  alias = "xiaoqi"
}

locals {
  ali_domain = true
  domain = "starmove.com"

  eip    = "1.1.1.1"
  sub_domains  = ["a", "b", "c"]

  exists_domain = local.ali_domain && "${length(data.alicloud_alidns_domains.domain[0].domains)}" > 0
}

data "alicloud_alidns_domains" "domain" {
  count = local.ali_domain ? 1 : 0
  domain_name_regex = format("^%s", local.domain)
  output_file       = "domains.txt"
}

resource "alicloud_alidns_domain" "dns" {
  count = local.exists_domain ? 0 : 1
  domain_name = local.domain
}

resource "alicloud_alidns_record" "default" {
  count       = length(local.sub_domains)
  domain_name = local.domain
  rr          = element(local.sub_domains, count.index)
  type        = "A"
  value       = local.eip
  depends_on = [alicloud_alidns_domain.dns]
}

resource "alicloud_alidns_record" "cust" {
  count       = length(local.sub_domains)
  provider    = "alicloud.xiaoqi"
  domain_name = "xiaoqibike.com"
  rr          = element(local.sub_domains, count.index)
  type        = "A"
  value       = local.eip
}
